package boun.center;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class WebDownloader {
	public static String fetchWebContent(String url) throws IOException {
	    return fetchWebContent(url, "UTF-8");
	}
	
	public static String fetchWebContent(String url, String charset) throws IOException {
        InputStream is = getInputStream(url, charset);
        return convertToString(is, charset);
	}

	private static InputStream getInputStream(String url, String charset) throws IOException {
		URL urlconn = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) urlconn.openConnection();
		//String charset = "UTF-8";
		//String charset = "ISO-8859-9";
		conn.setRequestProperty("Accept-Charset", charset);
		conn.connect();
		InputStream is = conn.getInputStream();
		return is;
	}

	private static String convertToString(InputStream is, String charset) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName(charset)));

        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
	}
}